
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
    <#include "/common/link.ftl">

</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="employee"/>
    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 10px;">
                    <form class="form-inline" id="searchForm" action="/employee/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <div class="form-group">
                            <label for="keyword">关键字:</label>
                            <input type="text" class="form-control" name="keyword" value="${qo.keyword}" placeholder="请输入姓名/邮箱">
                        </div>
                        <div class="form-group">
                            <label for="dept"> 部门:</label>
                            <select class="form-control" id="dept" name="deptId">
                                <option value="">全部</option>
                                <#--使用freemarket遍历-->
                                <#list departments as d>
                                    <#--选中时回显,可用下面方法写在option标签内,也可以用js的方法
                                        给上面的select标签添加value值
                                        ${(d.id == qo.deptId)?string('selected','')}
                                    -->
                                    <option value="${d.id}">${d.name}</option>
                                </#list>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary">
                            <span class="glyphicon glyphicon-search"></span> 查询
                        </button>
                        <a href="/employee/input" class="btn btn-success btn-input">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                        <a class="btn btn-danger btn-deleteList">
                            <span class="glyphicon glyphicon-trash"></span> 批量删除
                        </a>
                        <a href="/employee/exportXls?keyword=${qo.keyword}&deptId=${qo.deptId}" class="btn btn-warning" >
                            <span class="glyphicon glyphicon-download"></span> 导出
                        </a>
                        <a  class="btn btn-warning btn-import">
                            <span class="glyphicon glyphicon-upload"></span> 导入
                        </a>
                    </form>
                </div>
                <div class="box-body table-responsive ">
                <table class="table table-hover table-bordered table-striped">
                    <thead>
                    <tr>
                        <th><input type="checkbox" id="allCb" onchange="checkChange(this)"></th>
                        <th>编号</th>
                        <th>用户名</th>
                        <th>真实姓名</th>
                        <th>邮箱</th>
                        <th>年龄</th>
                        <th>部门</th>
                        <th>操作</th>
                        <th>状态</th>
                    </tr>
                    </thead>
                    <tbody>
                    <#--s使用freemarket的循环遍历-->
                    <#list pageInfo.list as e>
                        <tr>
                        <td><input type="checkbox" class="cb" value="${e.id}"></td>
                        <td>${e_index+1}</td>
                        <td>${e.username}</td>
                        <td>${e.name}</td>
                        <td>${e.email}</td>
                        <td>${e.age}</td>
                        <td>${e.dept.name}</td>
                        <td>${e.status?string("正常","禁用")}</td>
                        <td>
                            <a href="/employee/input?id=${e.id}" class="btn btn-info btn-xs btn_redirect">
                                <span class="glyphicon glyphicon-pencil"></span> 编辑
                            </a>
                            <a class="btn btn-danger btn-xs btn-delete" data-url="/employee/delete?id=${e.id}">
                                <span class="glyphicon glyphicon-trash"></span> 删除
                            </a>
                            <#if e.status>
                                <a class="btn btn-danger btn-xs btn-ban" data-flag='{"id":${e.id},"status":${(e.status)?string("true","false")}}'>
                                    <span class="glyphicon glyphicon-ban-circle"></span> 禁用
                                </a>
                            <#else>
                                <a class="btn btn-success btn-xs btn-ban" data-flag='{"id":${e.id},"status":${(e.status)?string("true","false")}}'>
                                    <span class="glyphicon glyphicon-pencil"></span> 恢复
                                </a>
                            </#if>
                        </td>
                    </tr>
                    </#list>
                    </tbody>
                </table>
                    <!--分页-->
                    <#include "/common/page.ftl">
                </div>

            </div>
        </section>
    </div>
    <#include "/common/footer.ftl">
</div>

<script>
    //给员工按部门查询回显数据,使用js的方法
    $('#dept').val(${qo.deptId});

    //按下全选/全不选按钮
    function checkChange(src){
        //获取点击后按钮状态
        var check = $(src).prop('checked');

        //给底下的所有input标签改成这个状态
        $(".cb").prop('checked',check);
    }

    //处理所有复选框选中时,全选/全不选按钮自动选中
    //使用页面加载完执行的函数,不是点击函数
    $(function () {
        //每次点击复选框时
        $(".cb").click(function () {
            //先定义一个标记
            var flag = true;

            //遍历这个标签
            $(".cb").each(function (i,ele) {
                //取checked状态
                var result = $(ele).prop('checked');

                //两个结果进行&&判断,只要出现一个false就是false,且每次点击都会重新遍历一次
                flag = flag && result;
            });

            //循环以后再把这个flag赋给全选/全不选的按钮
            $("#allCb").prop('checked',flag);
        });
    });

    //给批量删除按钮绑定点击事件
    $('.btn-deleteList').click(function () {
        var ids =[];
        //遍历所有input标签
        $(".cb").each(function (i,ele) {
            //判断checked状态
            if($(ele).prop('checked')){
                //如果是选中,则保存再数组中
                ids.push($(ele).val());
            }
        });
        //遍历完弹出确定框
        Swal.fire({
            title: '确定删除吗？',
            text: '你将无法恢复它！',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定',
            cancelButtonText: '取消',
        }).then((result) => {
            if (result.value){
                //发送请求
                window.location.href="/employee/deleteList?ids="+ids;
            }
        });
    });

    //点击导入弹出模态框
    $('.btn-import').click(function () {
        $('#importModal').modal('show');
    });

    //导入表单提交
    $('#importForm').ajaxForm(function (data) {
        if(data.success){
            Swal.fire({
                text: '成功',
                icon: 'success',
                confirmButtonText: '确定',
            }).then((result) => {
                if(result.value){
                    window.location.reload();
                }
            });
        }else {
            Swal.fire({
                text:'成功' + data.successCount + '条;' +
                '失败' + data.failCount + '条,' + data.msgList,
                confirmButtonText: '重新上传',
            }).then((result) => {
                if(result.value){
                    $('#importModal').modal('show');
                }
            });
        }
    });

    //点击禁用/恢复按钮,发送ajax设置
    $('.btn-ban').click(function () {
        //获取data-flag的值
        //发送ajax请求设置值
        $.post("/employee/status",$(this).data('flag'),function (data) {
            if(data.success){
                Swal.fire({
                    text: '设置成功',
                    icon: 'success',
                    confirmButtonText: '确定',
                }).then((result) => {
                    if(result.value){
                        window.location.reload();
                    }
                });
            }else {
                Swal.fire(
                    data.msg,
                    '',
                    'error'
                )
            }
        });
    });
</script>
<#--模态框-->
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">导入</h4>
            </div>
            <form class="form-horizontal" action="/employee/importXls" enctype="multipart/form-data" method="post" id="importForm">
                <div class="modal-body">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label"></label>
                        <div class="col-sm-6">
                            <#--application/vnd.openxmlformats-officedocument.spreadsheetml.sheet表示可上传xlsx-->
                            <input type="file" name="importFile" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <div class="col-sm-3"></div>
                        <div class="col-sm-6">
                            <a href="/xls/employee_import.xlsx" class="btn btn-success" >
                                <span class="glyphicon glyphicon-download"></span> 下载模板
                            </a>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary btn-submit">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>
